<?php

/**
 * This is the model class for table "invoice".
 *
 * The followings are the available columns in table 'invoice':
 * @property string $invoice_no
 * @property string $date_invoice
 * @property string $currency
 * @property string $due_date
 * @property string $PO_reference
 * @property string $status_delete
 */
class Invoice extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'invoice';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('invoice_no, date_invoice, currency, account_id due_date, PO_reference', 'required'),
			array('invoice_no, PO_reference', 'length', 'max'=>50),
			array('created_by', 'length', 'max'=>30),
			array('currency', 'length', 'max'=>5),
			array('status_delete', 'length', 'max'=>11),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('invoice_no, date_invoice, currency, due_date, PO_reference, status_delete', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'purchase' => array(self::BELONGS_TO, 'PurchaseOrderHeader', 'purchase_no'),
			'account' => array(self::BELONGS_TO, 'BankAccount', 'account_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'invoice_no' => 'Invoice No',
			'date_invoice' => 'Date Invoice',
			'currency' => 'Currency',
			'due_date' => 'Due Date',
			'PO_reference' => 'Po Reference',
			'account_id'=>'Account',
			'status_delete' => 'Status Delete',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('invoice_no',$this->invoice_no,true);
		$criteria->compare('date_invoice',$this->date_invoice,true);
		$criteria->compare('currency',$this->currency,true);
		$criteria->compare('due_date',$this->due_date,true);
		$criteria->compare('account_id',$this->account_id,true);
		$criteria->compare('PO_reference',$this->PO_reference,true);
		$criteria->compare('status_delete',$this->status_delete,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
		    	'defaultOrder'=>'invoice_no asc',
			)
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Invoice the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave() {

	    if (parent::beforeSave()) {

	        $this->status_delete='-';
	        $this->created_by=Yii::app()->session['id'];
	        $this->generate=0;
	        
	        return true;
	    }
	}
}
